package net.amsoft.iservice.management.webapp.struts.action;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.FileUploadUtil;
import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.struts.form.RelatedNewsUploadForm;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

/**
 * Gets called when an ibroker admin opts to change the related-news 
 * image for an iservice, selects an image and clicks "Submit" button <br>
 * 
 * @author mukta
 */
public class RelatedNewsUploadAction extends Action 
{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		// Process the FormFile
		RelatedNewsUploadForm myForm = (RelatedNewsUploadForm) form;

		if ((isCancelled(request)) || ("cancel".equals(myForm.getAction())))
		{
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Cancelled Related-News Image File Upload..");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to=" + IBrokerConstants.FWD_CANCEL);
			return mapping.findForward(IBrokerConstants.FWD_CANCEL);
		}
		
		// Check if user has come through proper channel or simply typed URL in browser
		FormFile myFile = myForm.getTheFile();
	    if (myFile==null)
	    {
            IServiceUtil.log(IServiceConstants.DEBUG, this, "execute(): redirect to=" + 
            		IBrokerConstants.FWD_LOGIN);
	        return mapping.findForward(IBrokerConstants.FWD_LOGIN);
	    }       
        IServiceUtil.log(IServiceConstants.INFO, this, 
                "execute(): Related-News Image File=" + myFile.getFileName());

		//write in file
		String serverPath = (String) request.getSession().getAttribute("ServerPath");
		String outfile = serverPath + "staging/" + (String) request.getSession().getAttribute("ServerName") + "_related-news.gif";
        ActionMessages oMsgs = new ActionErrors();
		try {
            IServiceUtil.log(IServiceConstants.INFO, this, 
                    "execute(): Calling \"FileUploadUtil.uploadFile()\"");
            FileUploadUtil.uploadFile("Related-News", myFile, outfile);
        } catch (FileNotFoundException e) {
            IServiceUtil.log(IServiceConstants.ERROR, this, 
                    "execute(): Exception=" + e.getMessage());
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, 
                    new ActionMessage(IBrokerConstants.ERR_FILE_NOT_FOUND));
            saveErrors(request.getSession(), oMsgs);
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to="+ mapping.getInputForward());
            return mapping.getInputForward();
        } catch (IOException e) {
            IServiceUtil.log(IServiceConstants.ERROR, this, 
                    "execute(): Exception=" + e.getMessage());
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, 
                    new ActionMessage(IBrokerConstants.ERR_IO_FAILED));
            saveErrors(request.getSession(), oMsgs);
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to="+ mapping.getInputForward());
            return mapping.getInputForward();
        }

        IServiceUtil.log(IServiceConstants.DEBUG, this, 
                "execute(): redirect to="+ IBrokerConstants.FWD_SUCCESS);
		return mapping.findForward(IBrokerConstants.FWD_SUCCESS);
	}
}